//
//  KZExamItemView.m
//  lg_zhinengbeikao_english
//
//  Created by 朱凯 on 2018/12/27.
//  Copyright © 2018 zhukai. All rights reserved.
//

#import "KZExamItemView.h"

#import "KZExamItemAnswerView.h"        // 作答视图

/**
 宏定义
 */
/// 滑块底部距离视图顶部最大距离
#define KZPanBlockMaxTopDistance    50
/// 滑块底部距离视图底部最大距离
#define KZPanBlockMaxBottomDistance 0


@interface KZExamItemView() <UIScrollViewDelegate>

/// 滑动方向
@property (nonatomic) int     scrollDirection;
/// 开始拖动滑动视图时，偏移的点，用于计算是往哪个方向滑动
@property (nonatomic) CGPoint scrollViewStartPosPoint;

/// 题干区滑动视图
@property (nonatomic, strong) UIScrollView *subjectAreaScroll;
/// 答题区滑动视图
@property (nonatomic, strong) UIScrollView *answerAreaScroll;

/// 滑块视图
@property (nonatomic, strong) UIView *panBlockView;
/// 刚开始拖动时的滑块底部位置
@property (nonatomic, assign) CGFloat theBottomWhilePanBegan;
@end

@implementation KZExamItemView
{
    UILabel *_testLabel;
}

#pragma mark - 初始化
- (instancetype)init {
    
    if (self = [super init]) {
        
        _scrollDirection = 0;
        [self setupUI];
    }
    return self;
}

#pragma mark - 创建视图
- (void)setupUI {
    
    _subjectAreaScroll = [UIScrollView new];
    [self addSubview:_subjectAreaScroll];
    [_subjectAreaScroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(0);
        make.width.equalTo(self);
        make.height.mas_equalTo(250);
    }];
    
    _testLabel = [UILabel new];
    _testLabel.font = FONT(100);
    _testLabel.textColor = UIColor.whiteColor;
    _testLabel.textAlignment = NSTextAlignmentCenter;
//    _testLabel.backgroundColor = rgba(arc4random_uniform(255),
//                                       arc4random_uniform(255),
//                                       arc4random_uniform(255),
//                                       1);
    [_subjectAreaScroll addSubview:_testLabel];
    [_testLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.height.top.width.equalTo(self.subjectAreaScroll);
        make.bottom.mas_equalTo(0);
    }];
    
    _answerAreaScroll = [UIScrollView new];
    _answerAreaScroll.delegate = self;
//    _answerAreaScroll.pagingEnabled = true;
    [self addSubview:_answerAreaScroll];
    [_answerAreaScroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.width.equalTo(self.subjectAreaScroll);
        make.top.equalTo(self.subjectAreaScroll.mas_bottom);
        make.bottom.equalTo(self);
    }];

    _panBlockView = [UIView new];
    _panBlockView.backgroundColor = UIColor.blackColor;
    [self addSubview:_panBlockView];
    [_panBlockView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(80);
        make.bottom.equalTo(self.answerAreaScroll.mas_top);
        make.centerX.equalTo(self);
    }];
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [_panBlockView addGestureRecognizer:pan];
}


#pragma mark swipe gesture
- (void)pan:(UIPanGestureRecognizer *)sender {
    
    // 拖动偏移量
    CGFloat panOffsetY = 0;
    switch (sender.state) {
        case UIGestureRecognizerStateBegan:
            // 拖动开始时记录滑块底部的位置
            _theBottomWhilePanBegan = _panBlockView.bottom;
            
            panOffsetY = 0;
            break;
        case UIGestureRecognizerStateChanged:
        {
            // 修改拖动偏移量，且偏移量需要满足偏移范围
            panOffsetY = [sender translationInView:self].y;
            CGFloat offsetY = _theBottomWhilePanBegan + panOffsetY;
            offsetY = MIN(self.height - KZPanBlockMaxBottomDistance, MAX(KZPanBlockMaxTopDistance, offsetY));
            
            // 更新滑块的约束，题干和作答区跟随变动
            [_panBlockView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(self.mas_top).offset(offsetY);
            }];
        }
            break;
        default:
            break;
    }
    
}

#pragma mark - UIScrollViewDelegate
#pragma mark - 控制滑动视图同时只能往一个方向滑动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (self.scrollDirection == 0){
        if (fabs(self.scrollViewStartPosPoint.x-scrollView.contentOffset.x)<
            fabs(self.scrollViewStartPosPoint.y-scrollView.contentOffset.y)){
            self.scrollDirection = 1;
        } else {
            self.scrollDirection = 2;
        }
    }
    if (self.scrollDirection == 1) {
        
        scrollView.contentOffset = CGPointMake(self.scrollViewStartPosPoint.x,scrollView.contentOffset.y);
    } else if (self.scrollDirection == 2){
        scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x,self.scrollViewStartPosPoint.y);
    }
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    self.scrollViewStartPosPoint = scrollView.contentOffset;
    self.scrollDirection = 0;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (decelerate) {
        self.scrollDirection =0;
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.scrollDirection =0;
}


#pragma mark - test
- (void)setIndex:(NSInteger)index {
    
    _index = index;
    _testLabel.text = [NSString stringWithFormat:@"%li", _index];
    
}
- (void)setItemNumbers:(NSArray *)itemNumbers {
    
    _itemNumbers = itemNumbers;
    
    
//    KZExamItemAnswerView *answerView = [[KZExamItemAnswerView alloc] initWithItemNumbers:itemNumbers type:KZAnswerClassFill];
//    answerView.backgroundColor = UIColor.redColor;
//    [_answerAreaScroll addSubview:answerView];
//    [answerView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.top.width.equalTo(self.answerAreaScroll);  
//        // 决定滑动视图的可滑动区域
//        make.bottom.mas_equalTo(-100);
//        // 高度内部决定
//    }];
    
//    UIView *last = nil;
//    for (int i = 0; i < _itemNumbers.count; i ++) {
//        UILabel *label = [UILabel new];
//        label.tag = i + 100;
//        label.font = FONT(100);
//        label.text = [NSString stringWithFormat:@"%@", _itemNumbers[i]];
//        label.textColor = UIColor.whiteColor;
//        label.textAlignment = NSTextAlignmentCenter;
//        label.backgroundColor = rgba(arc4random_uniform(255),
//                                     arc4random_uniform(255),
//                                     arc4random_uniform(255),
//                                     1);
//        [_answerAreaScroll addSubview:label];
//        [label mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.width.equalTo(self.answerAreaScroll);
//            make.height.mas_equalTo(400);
//            if (last) {
//                make.left.equalTo(last.mas_right);
//            } else {
//                make.left.equalTo(self.answerAreaScroll);
//            }
//        }];
//        last = label;
//    }
//    [last mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.mas_equalTo(0);
//        make.bottom.mas_equalTo(0);
//    }];
}

#pragma mark - 外部调用
- (void)seekToFirst {
    [UIView animateWithDuration:0.35 animations:^{
        self.answerAreaScroll.contentOffset = CGPointMake(0, 0);
    }];
}
- (void)seekToLast {
    [UIView animateWithDuration:0.35 animations:^{
        self.answerAreaScroll.contentOffset = CGPointMake(self.answerAreaScroll.contentSize.width - self.answerAreaScroll.width, 0);
    }];
}
- (void)seekToNumber:(NSInteger)number {
    
    NSInteger delta = number - [_itemNumbers.firstObject integerValue];
    
    [UIView animateWithDuration:0.35 animations:^{
        self.answerAreaScroll.contentOffset = CGPointMake(delta * self.answerAreaScroll.width, 0);
    }];
}

@end


